package org.jeecg.modules.demo.clock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.clock.entity.BusPointsClock;
import org.jeecg.modules.demo.clock.mapper.BusPointsClockMapper;
import org.jeecg.modules.demo.clock.service.IBusPointsClockService;
import org.jeecg.modules.demo.points.entity.BusVipPointDetail;
import org.jeecg.modules.demo.points.service.impl.BusVipPointDetailServiceImpl;
import org.jeecg.modules.demo.totalpoints.entity.BusVipPoint;
import org.jeecg.modules.demo.totalpoints.service.impl.BusVipPointServiceImpl;
import org.jeecg.modules.system.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 打卡记录表
 * @Author: jeecg-boot
 * @Date: 2021-07-17
 * @Version: V1.0
 */
@Service
public class BusPointsClockServiceImpl extends ServiceImpl<BusPointsClockMapper, BusPointsClock> implements IBusPointsClockService {


    @Autowired
    private BusVipPointServiceImpl busVipPointService;

    @Autowired
    private BusVipPointDetailServiceImpl busVipPointDetailService;

    @Override
    @Transactional
    public Boolean clock() {
        BusPointsClock busPointsClock = new BusPointsClock();
        Date mDate = new Date();
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = loginUser.getId();
        LambdaQueryWrapper<BusPointsClock> clockQueryWrapper = new LambdaQueryWrapper<BusPointsClock>();
        clockQueryWrapper.eq(BusPointsClock::getCreateBy, userId).eq(BusPointsClock::getCreateTime, mDate);
        List<BusPointsClock> clocks = listTodyMock(userId, mDate);
        if (clocks != null && clocks.size() > 0) {
            return false;
        }
        QueryWrapper<BusPointsClock> clockUpdateWrapper = new QueryWrapper<>();
        clockUpdateWrapper.eq("create_by", userId);

        BusPointsClock busPointsClock1 = getOne(clockQueryWrapper);
        if (busPointsClock1 != null) {
            busPointsClock = busPointsClock1;
            busPointsClock.setCreateBy(userId);
            busPointsClock.setCreateTime(mDate);
            updateById(busPointsClock);
        } else {
            busPointsClock.setCreateBy(userId);
            busPointsClock.setCreateTime(mDate);
            save(busPointsClock);
        }

        //更新积分
        QueryWrapper<BusVipPoint> pointsQueryWrapper = new QueryWrapper<>();
        pointsQueryWrapper.eq("user_id", userId);
        BusVipPoint busVipPoint = busVipPointService.getOne(pointsQueryWrapper);
        if (busVipPoint != null) {
            busVipPoint.setPoints(busVipPoint.getPoints() + 10);
            busVipPointService.updateById(busVipPoint);
        } else {
            BusVipPoint tpo = new BusVipPoint();
            tpo.setUserId(userId);
            tpo.setPoints(10);
            tpo.setCreateTime(mDate);
            tpo.setCreateBy(userId);
            busVipPointService.save(tpo);
        }
        BusVipPointDetail detail = new BusVipPointDetail();
        detail.setAmount(10);
        detail.setCreateBy(loginUser.getUsername());
        detail.setCreateTime(new Date());
        detail.setUserId(userId);
        detail.setInOut(1);
        busVipPointDetailService.save(detail);
        return true;
    }

    @Override
    public List<BusPointsClock> listTodyMock(String userId, Date date) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("mdate", date);
        return this.baseMapper.listTodyMock(params);
    }

}
